package cn.edu.neusoft.yuejian12.Fragment;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import cn.edu.neusoft.yuejian12.Adapter.TrainListAdapter;
import cn.edu.neusoft.yuejian12.JavaBean.Train;
import cn.edu.neusoft.yuejian12.Model.TrainModel;
import cn.edu.neusoft.yuejian12.R;

/**
 * Created by 木子李 on 2017/9/27.
 */

public class TrainFragment  extends Fragment {
    private RecyclerView recyclerView;
    private View view=null;
    private TrainModel trainModel;
    private TrainListAdapter trainListAdapter;
    private Button one,two,three,all;
    private TrainAllFragment trainAllFragment;
    private TrainOneFragment trainOneFragment;
    private TrainTwoFragment trainTwoFragment;
    private TrainThreeFragment trainThreeFragment;
    private FragmentManager fragmentManager;
    private List<ImageView> images;
    private FragmentTransaction transaction;
    private ViewPager train_vp;
    private FrameLayout train_frame;
    //当前图片的位置
    private int current;
    private Context context;
    //上一次图片的位置
    private int oldpos;
    private int[] imgResource={R.drawable.lunbo_one,R.drawable.lunbo_two,R.drawable.lunbo_three};
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        view=inflater.inflate(R.layout.tab02,container,false);
        initViews();
        initEvents();
        lunbo();
        return view;
    }
    private void initViews(){
        /*recyclerView = (RecyclerView)view.findViewById(R.id.train);
        recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
        recyclerView.setItemAnimator(new DefaultItemAnimator());*/
        one=(Button) view.findViewById(R.id.one);
        two=(Button)view.findViewById(R.id.two);
        three=(Button)view.findViewById(R.id.three);
        all=(Button)view.findViewById(R.id.all);
        train_frame=(FrameLayout)view.findViewById(R.id.train_frame);
        trainOneFragment=new TrainOneFragment();
        trainTwoFragment=new TrainTwoFragment();
        trainThreeFragment=new TrainThreeFragment();
        trainAllFragment=new TrainAllFragment();
        context=getActivity();
        fragmentManager=getChildFragmentManager();
        transaction=fragmentManager.beginTransaction();
        transaction.add(R.id.train_frame,trainAllFragment);
        transaction.commit();
        train_vp=(ViewPager)view.findViewById(R.id.train_vp);
        //创建List对象，用于装载ImageView对象
        images=new ArrayList<ImageView>();
        for (int i=0;i<imgResource.length;i++)
        {
            ImageView imageView=new ImageView(getActivity());
            imageView.setBackgroundResource(imgResource[i]);
            images.add(imageView);
        }

    }

    private void initEvents(){
        three.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                replace(trainThreeFragment);
            }
        });
        one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                replace(trainOneFragment);
            }
        });
        two.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                replace(trainTwoFragment);
            }
        });
        all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                replace(trainAllFragment);
            }
        });
      /*  TrainModel trainModel = new TrainModel();
        Call call = trainModel.train();
        call.enqueue(new Callback<List<Train>>() {
            @Override
            public void onResponse(Call<List<Train>> call, Response<List<Train>> response) {
                List<Train> train = response.body();
                recyclerView.setAdapter(new TrainListAdapter(getActivity(), train));
            }

            @Override
            public void onFailure(Call<List<Train>> call, Throwable t) {
                Toast.makeText(getActivity(),"连接失败", Toast.LENGTH_SHORT).show();


            }
        });*/
    }
    private void lunbo() {
        train_vp.setAdapter(pageadapter);
    }
    PagerAdapter pageadapter=new PagerAdapter() {
        @Override
        public int getCount() {
            return images.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(images.get(position));
            return images.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(images.get(position));
        }
    };
    class  PageTask implements Runnable{
        @Override
        public void run() {
            current=(current+1)%imgResource.length;
            handler.sendEmptyMessage(current);
        }
    }
    private Handler handler = new Handler(){
        public void handleMessage(android.os.Message msg) {
            train_vp.setCurrentItem(current);
        };
    };

    @Override
    public void onStart() {
        super.onStart();
        Timer timer=new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                current=(current+1)%imgResource.length;
                handler.sendEmptyMessage(current);
            }
        },3000,3000);

    }

    private void  replace(Fragment fragment)
    {
        transaction=fragmentManager.beginTransaction();
        transaction.replace(R.id.train_frame,fragment);
        transaction.commit();
    }

    }
